#!/bin/bash

# what it needs:
# ffmpeg
# wav2png https://github.com/beschulz/wav2png
# img2txt is from apt-get install caca-utils

# what it does:
# draws some simple blocky audio waveforms in terminal


# checks
which ffmpeg >/dev/null 2>&1   || { echo >&2 "I require ffmpeg but it's not installed.  Aborting."; exit 1; }
wav2png -v foo >/dev/null 2>&1 || { echo >&2 "I require wav2png but it's not installed.  Aborting."; exit 1; }
img2txt -v foo >/dev/null 2>&1 || { echo >&2 "I require img2txt but it's not installed.  Aborting."; exit 1; }


while [ $# -gt 0 ]; do

	# make temporary folder and trap to clean up
	TMPDIR=`mktemp -d`
	trap "rm -rf $TMPDIR" EXIT

	# expand path, so this can be used from cli as well (on relative paths)
	# expanded=$(readlink -f "$1")
	expanded=$1

	# ffmpeg decode to tmp wav
	ffmpeg -nostats -i "$expanded" -vn "$TMPDIR/tmp.wav" >/dev/null 2>&1

	# wav2png convert to tmp png
	# wav2png -w 1600 -h 500 -b ffffffff -f 00000000 "$TMPDIR/tmp.wav" >/dev/null 2>&1
	wav2png -w 1600 -h 500 -b 000000ff -f ffffffff "$TMPDIR/tmp.wav" >/dev/null 2>&1
	
	#feh "$TMPDIR/tmp.wav.png" & sleep 3 
	#killall feh

	# img2txt echo text
	
	img2txt -d none -f utf8 -W 150 -H 15 "$TMPDIR/tmp.wav.png" 
	



	# cleanup
	rm -rf $TMPDIR



    shift
done
